package com.koala.mylittlesecretary;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import android.content.Context;
import android.util.Log;

public class LoadAndSave {

	// hardcoded
	String filename_serial = "mlsciser";

	public void serializeCI(ContactItems myCI) {
		try {
			FileOutputStream fStream = MainActivity.mainAct.openFileOutput(
					filename_serial, Context.MODE_PRIVATE);
			ObjectOutputStream oStream = new ObjectOutputStream(fStream);

			oStream.writeInt(myCI.iCountryCode);
			oStream.writeObject(myCI.hmNameToNumber);
			oStream.writeObject(myCI.hmNumberToName);
			oStream.writeObject(myCI.hmNumberToProfile);
			oStream.flush();
			oStream.close();

			Log.v("MLS", "Serialize success");
		} catch (Exception e) {
			Log.v("MLS", "Error at serializing: " + e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public ContactItems deserializeCI() {
		ContactItems myCI = null;
		try {
			FileInputStream fStream = MainActivity.mainAct
					.openFileInput(filename_serial);
			ObjectInputStream oStream = new ObjectInputStream(fStream);
			myCI = new ContactItems(oStream.readInt());
			myCI.hmNameToNumber = (HashMap<String, String>) oStream
					.readObject();
			myCI.hmNumberToName = (HashMap<String, String>) oStream
					.readObject();
			myCI.hmNumberToProfile = (HashMap<String, Integer>) oStream
					.readObject();
		} catch (Exception e) {
			Log.v("MLS", "Error at deserializing: " + e.getMessage());
		}

		return myCI;
	}

	public LoadAndSave() {
		// TODO Auto-generated constructor stub
	}
}
